﻿/// <reference path="../jquery/jquery-1.8.0-vsdoc.js" />
/// <reference path="flowItems.js" />


var isLoad = false;

var url = "";

/*Jquery页面加载*/
$(function () {

    LoadMenu();

    LoadTab();

    Fixed();

    //2--fancybox
    $("a#flow").fancybox({ 'width': '100%', 'height': '100%' });
    //3--切换Tab 
    $("#flowCategorys").tabs({ "onSelect": function (title, index) { switchTab(title, index) } });

    isLoad = true;
});

/*发起流程按钮*/
function createFlow() {

    if ($("#btnLaunchFlow").attr("title")) {
        alert("没有权限发起该流程!");
        return;
    }

    if (url == "") {
        //alert("跳转到发起流程页面");
    } else {
        window.open(url, "_blank")
    }
}

/*点击流程名称显示流程图*/
function showFlow(tabIndex, obj) {

    //obj is jquery object

    var id = obj[0].id;

    $(".selectedFlow").removeClass("selectedFlow");
    obj.addClass("selectedFlow");

    //data

    var data = flowItems1;

    if (tabIndex == 2) {
        data = flowItems2;
    }
    if (tabIndex == 3) {
        data = flowItems3;
    }

    var item = {};

    for (var i = 0; i < data.length; i++) {
        if (data[i].id == id) {
            item = data[i];
            break;
        }
    }
    if (item.id) {
        $(".flow_title").html(shortTitle(item.name));

        if (item.chart == "0") {
            $("#flow").attr("href", "Resource/Flow/big/暂无流程图(大).jpg");
            $("#chartImg").attr("src", "Resource/Flow/small/暂无流程图(小).jpg");
        } else if (item.big_img) {
            $("#flow").attr("href", item.big_img);
            $("#chartImg").attr("src", item.small_img);
        }
        else {
            //根据命名规则
            $("#flow").attr("href", "Resource/Flow/big/" + item.name + ".jpg");
            $("#chartImg").attr("src", "Resource/Flow/small/" + item.name + ".jpg");
        }

    }
    //链接
    if (item.flowId) {
        if (item.page) {
            url = "/OAWeb/FounderOAResourceGroup/Modules/Workflow/" + item.page + ".aspx?AppCode=" + item.flowId + "&ActionType=0";
        }
        else {
            url = "";
        }
    }
    else if (item.url) {
        url = item.url;
    }
    else {
        url = "";
    }

    //没有流程ID
    if (item.flowId == 0) {
        $("#btnLaunchFlow").css({ "background": '#67A00D' });
        $("#btnLaunchFlow").attr("title", "");
    }
    else {
        if (item.flowId) {
            //Ajax请求
            checkPermissions(item);
        }
    }
}

/*切换Tab*/
function switchTab(title, index) {

    if (isLoad) {
        showFlow(index + 1, $("#" + (index + 1) + "101"));
    }
}

function shortName(name, shortLength) {
    return name.length > shortLength ? name.substr(0, shortLength - 1) + "..." : name;
}

function shortTitle(title) {
    return title.length > 16 ? title.substr(0, 16) + "..." : title;
}


function LoadTab() {
    //Tab1---绑定流程的事件
    var thirdColumn = $(".thirdColumn");
    var data = flowItems1;
    for (var i = 0; i < thirdColumn.length; i++) {
        var column = $(thirdColumn[i]);
        for (var j = 0; j < data.length; j++) {
            if (data[j].row == i + 1) {
                //显示流程列表
                column.append("<span id=" + data[j].id + " title=" + data[j].name + " class=\"thirdSpan\">" + shortName(data[j].name, 8) + "</span>");
            }
        }
    }
    //绑定点击事件
    $(".thirdSpan").on("click", function () { showFlow("1", $(this)) });

    //Tab2
    var fifthColumn = $(".fifthColumn");
    data = flowItems2;
    for (var i = 0; i < fifthColumn.length; i++) {
        var column = $(fifthColumn[i]);
        for (var j = 0; j < data.length; j++) {
            if (data[j].row == i + 1) {
                //显示流程列表
                column.append("<span id=" + data[j].id + " title=" + data[j].name + " class=\"fifthSpan\">" + shortName(data[j].name, 14) + "</span>");
            }
        }
    }
    //绑定点击事件
    $(".fifthSpan").on("click", function () { showFlow("2", $(this)) });

    //Tab3
    var seventhColumn = $(".seventhColumn");
    data = flowItems3;
    for (var i = 0; i < seventhColumn.length; i++) {
        var column = $(seventhColumn[i]);
        for (var j = 0; j < data.length; j++) {
            if (data[j].row == i + 1) {
                //显示流程列表
                column.append("<span id=" + data[j].id + " title=" + data[j].name + " class=\"seventhSpan\">" + shortName(data[j].name, 14) + "</span>");
            }
        }
    }
    //绑定点击事件
    $(".seventhSpan").on("click", function () { showFlow("3", $(this)) });
}

function LoadMenu() {


    $("#nav li").mouseover(function () {
        $(this).css("background-color", "#f2f2f2");
    }).mouseout(function () {
        $(this).css("background-color", "white");
    });
    
    $("#menus li:even").mouseover(function () {
        $(this).css("background-color", "white");
        $(this).find("a").css("color", "#d80c17");
    }).mouseout(function () {
        $(this).css("background-color", "#d80c17");
        $(this).find("a").css("color", "white");
    });
}

function Fixed() {

    //固定
    var ie6 = /msie 6/i.test(navigator.userAgent), dv = $('#desc'), st;
    var left = dv.offset().left;
    dv.attr('otop', dv.offset().top);
    $(window).scroll(function () {

        if ($("#flowCategorys").height() <= $("#desc").height()) {
            return;
        }

        st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
        if (st >= parseInt(dv.attr('otop'))) {
            if (ie6) {//IE6不支持fixed属性，所以只能靠设置position为absolute和top实现此效果
                dv.css({ position: 'absolute', top: st });
            }
            else if (dv.css('position') != 'fixed') dv.css({ 'position': 'fixed', top: 0, left: left });
        } else if (dv.css('position') != 'static') dv.css({ 'position': 'static' });
    });
}

function checkPermissions(item) {

    $.get("Ajax.ashx", { "flowId": item.flowId }, function (data) {

        //log
        /*
        if (window.console) {
            window.console.log("callback data:" + data);
        }
        */

        var json = eval("(" + data + ")");
        if (json.name) {
            $("#userInfo").text("欢迎您，" + json.code + "(" + json.name + ")");
        }
        else {
            $("#userInfo").text("欢迎您");
        }

        //禁用按钮，tip
        if (json.flag == 0) {
            $("#btnLaunchFlow").css({ "background": '#a9a9a9' });
            $("#btnLaunchFlow").css({ "color": 'white' });
            $("#btnLaunchFlow").attr("title", "没有权限发起该流程");
        }
        else {
            $("#btnLaunchFlow").css({ "background": '#67A00D' });
            $("#btnLaunchFlow").attr("title", "");
        }
    });
}